$(document).ready(function(){
	var isModerator = false;
	var moderatorName;
	var moderatorId;
	$.notify_osd.setup({
		'sticky'      : false,                 
		'timeout'     : 5,                     
		'dismissable' : true,
		'click_through': true
	});
	
	
	
//--------------------------------------------------
//Client callback functions
//--------------------------------------------------	
	now.receiveMessage = function(name, message){
		chat.addChatMessage(name, message);
	}
	now.receiveModerator = function(bool, moderator, name) {
		console.log("setting isModerator: " + bool);
		if (bool) {	
			$.notify_osd.create({
				'text'        : 'You are the moderator'
			});
		} else if (moderatorId != moderator) {
			if (moderator && (moderator != "null")) {
				$.notify_osd.create({
					'text'        : name + ' is now the moderator'
				});
			}
		}
		isModerator = bool;
		moderatorId = moderator;
		moderatorName = name;
		now.distributeAllMembers();
	}
	now.receiveMemberList = function(members, moderator) {
		console.log("Members: " + members);
		console.log("Moderator: " + moderator);
		chat.setMemberList(members, moderator);
	}
	now.receiveMemberLocations = function(id, name, lat, lng) {
		console.log("Deleting overlays");
		deleteOverlays();
		console.log("Location of " + name + ": " + lat + ", " + lng);
		addLocMarker(id, name, lat, lng);
		
		if (getNumPeopleOnMap > 1) {
			var midPos = getMiddle(locArray);
			addMidMarker(midPos);
		}
	}
	now.receiveSearch = function(moderatorId, searchVal, data) {
		console.log("Search has been initiated by user " + moderatorId + " for: " + searchVal);
		onSearchResult(data);
	}
	
	
//--------------------------------------------------
//Call server stuff
//--------------------------------------------------

	$("#send-button").click(function(){
		now.distributeMessage($("#text-input").val());
		$("#text-input").val("");
	});
	
	$("#text-input").keypress(function(event) {
	  if ( event.which == 13 ) {
	     now.distributeMessage($("#text-input").val());
		$("#text-input").val("");
	   }
	});
	
	
	sendSearchToServer = function(middleLat, middleLng, query, category) {
		if (isModerator) {
			console.log("Searching for: " + category);
			now.distributeSearch(middleLat, middleLng, query, category);
		} else {
			console.error("User is not the moderator, shouldn't be able to use the search");
		}
	}
	
	initLocation = function() {
		now.lat = getCurrentLat();
		now.lng = getCurrentLng();
		console.log("currLat: " + now.lat + ", currLng: " + now.lng);
		
		now.distributeAllLocations();
	}
	
	
	//Set new moderator if this client is the current moderator
	$("#new-moderator-button").click(function(){
		if (isModerator) {
			now.chooseNewModerator();
		} else {
			console.error("User is not the moderator, shouldn't be able to set a new moderator");
		}
		now.distributeMessage($("#text-input").val());
	});

	now.ready(function(){
		now.name = prompt("What's your name?", "");
		if (!now.name) now.name = "Anonymous User";
		console.log("name: " + now.name);
		
		now.distributeAllMembers();	
		
		initLocation();
		
	});


});